//
//  FANTabBar.swift
//  Weibo - Swift
//
//  Created by user on 16/4/16.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

class FANTabBar: UITabBar {

    var callAddButton:(()->())?
   
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(addButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        self.addButton.center = CGPointMake(self.frame.size.width/2,self.frame.size.height/2)
        
        let subWidth = UIScreen.mainScreen().bounds.size.width/5
        
        var index:CGFloat = 0
        
        for value in self.subviews{
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = subWidth;
                
                value.frame.origin.x = CGFloat(subWidth) * index
                
                index++
                
                if index == 2{
                    
                    index++
                }
            }
        }
        
    }
    
    func didClickAddButton(){
        
        callAddButton!()
        
        
    }
    
    
    private lazy var addButton:UIButton = {
       
        let button = UIButton()
        
        button.addTarget(self, action: "didClickAddButton", forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        return button
    }()
}
